<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" @click="showAddCateDialog">添加商品</el-button>
      <tree-grid
      :data="goodsCate"
      :columns="columns"
      border
      stripe
      show-index
      :expand-type="false"
      :selection-type="false"
      index-text="#">
        <!-- 是佛有效模板 -->
        <template slot-scope="scope" slot="isOk">
          <!-- 有效 -->
          <i style="color: green" v-if="!scope.row.cat_deleted" class="el-icon-success"></i>
          <!-- 无效 -->
          <i style="color: red" v-else class="el-icon-error"></i>
        </template>
        <!-- 排序模板 -->
        <template slot-scope="scope" slot="order">
         {{ scope.row.cat_level }}
         <el-tag v-if="scope.row.cat_level === 0">等级1</el-tag>
         <el-tag v-else-if="scope.row.cat_level === 1" type="success">等级2</el-tag>
         <el-tag v-else type="warning">等级3</el-tag>
        </template>
        <!-- 操作模板 -->
        <template slot="options">
         <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
         <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-grid>
      <!-- 分页 -->
      <el-pagination
        @size-change="handSizeChange"
        @current-change="handCurrentChange"
        :current-page="queryDate.pagenum"
        :page-sizes="[2, 4, 6, 8, 10]"
        :page-size="queryDate.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- 添加对话框 -->
      <el-dialog
        title="提示"
        :visible.sync="addCateDialogVisible"
        width="50%"
        @close="closeAddCateDialog">
        <el-form ref="addCateForm" :rules="addCateRules" :model="addCateForm" label-width="120px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类名称">
            <!-- 既可以设置默认值，级联选择器发生变化，最新值也会保存给cascaderValue
            ：options渲染级联选择器的所有数据
            ：props 配置对象，可以配置一些属性
             -->
            <el-cascader
            clearable
               v-model="cascaderValue"
              :options="parentCate"
              :props="cascaderProps"
              @change="cascaderChange">
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCateSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryDate: {
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      goodsCate: [],
      // 用来配置树形表格的每列
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'options'
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 添加分类表单的校验规则
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCate: [],
      cascaderValue: '',
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name', // 将哪个属性渲染到页面
        value: 'cat_id', // 将哪个属性保存下来
        checkStrictly: true
      }
    }
  },

  created () {
    this.getGoodsCate()
  },

  methods: {
    async getGoodsCate () {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryDate
      })
      // 获取成功console
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.total = res.data.total
      this.goodsCate = res.data.result
    },
    handSizeChange (val) {
      this.queryDate.pagesize = val
      this.getGoodsCate()
    },
    handCurrentChange (val) {
      this.queryDate.pagenum = val
      this.getGoodsCate()
    },
    // 点击添加分类
    async showAddCateDialog () {
      this.addCateDialogVisible = true
      this.addCateDialogVisible = true
      const { data: res } = await this.$axios.get('categories', {
        params: {
          type: 2
        }
      })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取分类列表失败')
      this.parentCate = res.data
    },
    // 级联选择器发生变化
    cascaderChange () {
      // 选择之后的父分类id this.cascaderVlaue
      // cat_pid: 0, cat_level: 0
      // 如果选择了一个cat_pid 变成选择的这个id cat_level：1
      // 如果选择了2个cat-pid 变成选择的最后这个id cat_level： 2
      // console.log(this.cascaderValue)
      if (this.cascaderValue.length !== 0) {
        this.addCateForm.cat_level = this.cascaderValue.length
        this.addCateForm.cat_pid = this.cascaderValue[this.cascaderValue.length - 1]
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      // console.log(this.cascaderValue)
      // console.log(this.addCateForm)
    },
    // 关闭对话框
    closeAddCateDialog () {
      this.$refs.addCateForm.resetFields()
      this.cascaderValue = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    // 点击确定添加分类
    addCateSubmit () {
      this.$refs.addCateForm.validate(async flag => {
        if (!flag) return
        const { data: res } = await this.$axios.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('创建分类失败')
        this.$message.success('创建分类成功')
        this.addCateDialogVisible = false
        this.getGoodsCate()
      })
    }
  }
}
</script>

<style scoped lang='less'>
.el-cascader {
  width: 100%;
}
</style>
